using System;
#if WINDOWS
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

#elif WINDOWS_PHONE
using System.Xml.Serialization;
#endif
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using System.Xml;
using System.IO.IsolatedStorage;
using System.IO;
#endif
namespace GameFirst
{
    public class Pausa : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D sfondo;
        SpriteBatch spriteBatch;
        #if WINDOWS
        Texture2D mirino;
        #endif
        Texture2D ritorna;
        Texture2D savegame;
        Texture2D esci;
        Texture2D ritornas;
        Texture2D savegames;
        Texture2D escis;
        Texture2D menu;
        Texture2D menus;
        bool sg;
        bool rt;
        bool ex;
        bool mn;
        bool pause;
        renderingdata r2;
        SoundEffect song;
        SoundEffectInstance songI;
        public Pausa(Game game)
            : base(game)
        {
            sg = false;
            rt = false;
            ex = false;
            mn = false;
            pause = true;
        }

        public override void Initialize()
        {
            r2 = (renderingdata)Game.Services.GetService(typeof(renderingdata));
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
#if WINDOWS
            mirino = Game.Content.Load<Texture2D>("mirino");
#endif
            sfondo = Game.Content.Load<Texture2D>("PAUSA");
            ritorna = Game.Content.Load<Texture2D>("return");
            esci = Game.Content.Load<Texture2D>("exit");
            savegame = Game.Content.Load<Texture2D>("savegame");
            ritornas = Game.Content.Load<Texture2D>("returns");
            escis = Game.Content.Load<Texture2D>("exits");
            savegames = Game.Content.Load<Texture2D>("savegames");
            menu = Game.Content.Load<Texture2D>("menub");
            menus = Game.Content.Load<Texture2D>("menubs");

            song = Game.Content.Load<SoundEffect>("menu");
            songI = song.CreateInstance();
            songI.IsLooped = true;
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (pause)
            {
                if (r2 == null)
                {
                    r2 = (renderingdata)Game.Services.GetService(typeof(renderingdata));
                }
                //AUDIO OPZIONI
                songI.Volume = menucomp.SongVol;
                if (songI.State != SoundState.Playing)
                    songI.Play();

                sg = false;
                rt = false;
                ex = false;
                mn = false;

                if ((InputGame.InputManagerGame.GetState().mouseX < ((int)(GraphicsDevice.Viewport.Width / 2.9f) + (int)(GraphicsDevice.Viewport.Width / 3.5f))) && (InputGame.InputManagerGame.GetState().mouseX > ((int)(GraphicsDevice.Viewport.Width / 2.9f))))
                {
                    //return
                    if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 2.6f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)(GraphicsDevice.Viewport.Height / 2.6f)))
                    {
                        rt = true;
                        
                        if (InputGame.InputManagerGame.GetState().click)
                        {
                            r2.PauX = InputGame.InputManagerGame.GetState().mouseX;
                            r2.PauY = InputGame.InputManagerGame.GetState().mouseY;
                            songI.Stop();
                            r2.giainpausa = false;
                            pause = false;
                            r2.finepausa = true;
                        }
                    }
                    //savegame
                    if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 1.9f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)(GraphicsDevice.Viewport.Height / 1.9f)))
                    {
                        sg = true;
                        if (InputGame.InputManagerGame.GetState().click)
                        {
                            AirSimulation.statechange.EspX = -1000.0f;
                            AirSimulation.statechange.EspY = -1000.0f;
                            GameLogic.GameSave();
                        }
                    }
                    //menu
                    if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 1.45f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)(GraphicsDevice.Viewport.Height / 1.45f)))
                    {
                        mn = true;
                        
                        if (InputGame.InputManagerGame.GetState().click)
                        {
                            songI.Stop();
                            while (Game.Components.Count > 0)
                            {
                                ((GameComponent)Game.Components[0]).Dispose();
                            }

                            Game.Components.Clear();

                            Game.Components.Add(new menu(Game));
                            Game.Components.Add(new InputGame.InputManagerGame(Game));
                        }
                    }
                    //exit
                    if ((InputGame.InputManagerGame.GetState().mouseY < ((int)(GraphicsDevice.Viewport.Height / 1.2f) + GraphicsDevice.Viewport.Height / 8)) && (InputGame.InputManagerGame.GetState().mouseY > (int)(GraphicsDevice.Viewport.Height / 1.2f)))
                    {
                        ex = true;
                        if (InputGame.InputManagerGame.GetState().click)
                        {
                            songI.Stop();
                            Game.Exit();
                        }
                    }
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (pause)
            {
                spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
                            #if WINDOWS
                spriteBatch.Draw(sfondo, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 1324, 883), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.1f);
                #elif WINDOWS_PHONE
                spriteBatch.Draw(sfondo, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 800, 480), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.1f);
                #endif
                if (rt == true)
                    spriteBatch.Draw(ritornas, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 2.6f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                else
                    spriteBatch.Draw(ritorna, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 2.6f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                if (sg == true)
                    spriteBatch.Draw(savegames, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.9f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                else
                    spriteBatch.Draw(savegame, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.9f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                if (mn == true)
                    spriteBatch.Draw(menus, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.45f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                else
                    spriteBatch.Draw(menu, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.45f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                if (ex == true)
                    spriteBatch.Draw(escis, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.2f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                else
                    spriteBatch.Draw(esci, new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.9f), (int)(GraphicsDevice.Viewport.Height / 1.2f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);

                #if WINDOWS
                spriteBatch.Draw(mirino, new Vector2(InputGame.InputManagerGame.inputState.mouseX - mirino.Width * 0.5f, InputGame.InputManagerGame.inputState.mouseY - mirino.Height * 0.5f), Color.White);
                #endif
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
